//
//  WithdrawVM.swift
//  LeTu
//
//  Created by g1 on 2024/1/4.
//

import Foundation

enum PayPlatformType {
    case wechat
    case ali
    
    func getName() -> String {
        switch self {
        case .wechat:
            return "微信"
        case .ali:
            return "支付宝"
        }
    }
}

struct PlatformItem {
    
    var type = PayPlatformType.wechat
    var icon: String
    var title: String
    var details: String
    
}

class WithdrawVM {
    
    private(set) var pricesArr = [WithdrawPriceModel]()
    
    // MARK: - 微信提现
    func withdrawWechat(money: Int, completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .withdrawWechat(params: ["money": money])) { json, jsonArr, code in
            MYLog("微信提现：",json)
            
            var dialog = DialogMessage()
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
                
    }
    
    // MARK: - 绑定微信
    func bindWx(params: [String: Any], completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .bindWX(params: params)) { json, jsonArr, code in
            MYLog("绑定微信：",json)
            var dialog = DialogMessage()
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            completeHandle(dialog)
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
    // MARK: - 绑定微信
    func unBindWX(completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .unBindWX) { json, jsonArr, code in
            MYLog("解绑微信：",json)
            var dialog = DialogMessage()
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            completeHandle(dialog)
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
    // MARK: - 获取提现价格列表
    func getWithdrawPrices(completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .getWithdrawPrices) { [weak self] json, jsonArr, code in
            
            MYLog("获取提现价格列表: ", json)
            
            var dialog = DialogMessage()

            guard let strongSelf = self, let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let dataDic = response.data as? [[String: Any]] else { return completeHandle(dialog) }
            strongSelf.pricesArr = Mapper<WithdrawPriceModel>().mapArray(JSONArray: dataDic)

            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
}
